const HotKeys = {};


export function registerHotKey(key, callback, options) {
  HotKeys[key] = HotKeys[key] || [];
  HotKeys[key].push({
    callback,
    options
  });
}

export function setupHotkey(container) {
  console.log(container);
  container.onkeydown = function (e) {
    const { keyCode } = e;
  
    const listeners = HotKeys[keyCode];

    if (listeners == null || listeners.length === 0) return;
    
    e.stopPropagation();
    e.preventDefault();

    let listenerCalled = false;
    ["ctrlKey", "altKey"].forEach((optionalKey) => {
      if (e[optionalKey]) {
        const listener = listeners.find((listener) => listener.options?.[optionalKey]);
        if (listener) {
          listenerCalled = true;
          listener.callback();
        }
      }
    });

    if (listenerCalled) return;

    const directListener = listeners.find((listener) => listener.options == null);
    directListener?.callback();
    
  };
}
